package com.example.s.Activity.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.lifecycle.Observer;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.s.Activity.BaseActivity;
import com.example.s.MainActivity;
import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.application.MyApplication;
import com.example.s.bean.User;
import com.example.s.network.OnMyNetworkListener;
import com.example.s.utils.LiveDataBus;
import com.example.s.utils.MyUtils;
import com.example.s.view.MyDialog;
import com.qmuiteam.qmui.widget.QMUILoadingView;

import org.json.JSONObject;

import java.util.HashMap;

public class LoginActivity extends BaseActivity {
    private ImageView back;
    private EditText account;
    private EditText password;
    private TextView forgetPwd;
    private ImageView seePwd;
    private TextView login;
    private final boolean[] isFinished = new boolean[2];
    boolean isSee = false;

    private void initView() {

        back = (ImageView) findViewById(R.id.back);
        account = (EditText) findViewById(R.id.account);
        password = (EditText) findViewById(R.id.password);
        forgetPwd = (TextView) findViewById(R.id.forgetPwd);
        seePwd = (ImageView) findViewById(R.id.seePwd);

        login = (TextView) findViewById(R.id.login);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        login.setBackgroundResource(R.drawable.login_btn_unable_bg);
        LiveDataBus.getInstance().with("loginIsFinished", boolean[].class).setValue(isFinished);
        LiveDataBus.getInstance().with("loginIsFinished", boolean[].class).observe(this, new Observer<boolean[]>() {
            @Override
            public void onChanged(boolean[] booleans) {
                for (boolean aBoolean : booleans) {
                    if (!aBoolean) {
                        login.setBackgroundResource(R.drawable.login_btn_unable_bg);
                        return;
                    }
                }
                login.setBackgroundResource(R.drawable.login_btn_bg);
            }
        });
        account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isFinished[0] = s.length() != 0;
                LiveDataBus.getInstance().with("loginIsFinished", boolean[].class).setValue(isFinished);
            }
        });//0
        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                isFinished[1] = s.length() != 0;
                LiveDataBus.getInstance().with("loginIsFinished", boolean[].class).setValue(isFinished);
            }
        });//1
        seePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: " + isSee);
                if (!isSee) {
                    password.setTransformationMethod(HideReturnsTransformationMethod
                            .getInstance());  //密码以明文显示
                    v.setBackgroundResource(R.mipmap.eye_icon_close);
                    isSee = true;
                } else {
                    password.setTransformationMethod(PasswordTransformationMethod
                            .getInstance());  //以密文显示，以.代替
                    v.setBackgroundResource(R.mipmap.eye_icon);
                    isSee = false;
                }
            }
        });

        forgetPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, ForgetPasswordActivity.class));
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyUtils.getInstance().isHasEmpty(
                        account.getText().toString().trim(),
                        password.getText().toString().trim()
                )) {
                    Toast.makeText(A, "请输入完整内容！", Toast.LENGTH_SHORT).show();
                    return;
                }

                HashMap<String, Object> map = getMap();
                map.put("id", account.getText().toString().trim());
                map.put("password", password.getText().toString().trim());
                N.post("/api/login", map, new OnMyNetworkListener() {
                    @Override
                    public void success(JSONObject response) {
                        startActivity(MainActivity.class);
                        finish();
                        String userInfo = response.optJSONObject("data").optJSONArray("data").optString(0);
                        String token = response.optJSONObject("data").optString("token");
                        MyApplication application = MyApplication.getInstance(A);
                        User user = User.objectFromData(userInfo);
                        application.setUser(user);
                        getSharedPreferences("user").edit().putString("token", token).commit();
//                        Log.d(TAG, "success: " + userInfo);
//                        Log.d(TAG, "success: " + token);
                    }

                    @Override
                    public void fail(JSONObject response) {
                        Toast.makeText(LoginActivity.this, "登陆失败，请检查后重试！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

}
